import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Wel from '../components/Welcome.vue'
import Users from '../components/user/Users.vue'
import Roles from '../components/rules/Roles.vue'
import Right from '../components/rules/Rights.vue'
import Categories from '../components/goods/Categories.vue'
import Goods from '../components/goods/Goods.vue'
import Add from '../components/goods/Add.vue'
import Params from '../components/goods/Params.vue'
import Orders from '../components/books/Orders.vue'
import Reports from '../components/statistics/Reports.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home', component: Home, redirect: '/welcome', children: [
      { path: '/welcome', component: Wel },
      { path:'/users', component: Users },
      { path:'/roles', component: Roles },
      { path:'/rights', component: Right },
      { path:'/categories', component: Categories},
      { path:'/goods', component: Goods},
      { path:'/params', component: Params},
      { path:'/orders', component: Orders},
      { path:'/reports', component: Reports},
      { path:'/goods/add', name:'addGoods',component: Add}
    ]
  }
]

const router = new VueRouter({
  routes
})
//挂载路由守卫 to: 将要访问的路径 from: 从哪个路径跳转而来 next: 是个函数， next() 放行 next('/login')强制跳转
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    return next()
  }
  let token = window.sessionStorage.getItem('token');
  if (!token) {
    return next('/login')
  }
  return next();
})
export default router
